//@author A0084556W
package Database;

import java.io.File;
import java.io.IOException;
import java.util.Vector;

import Main.Item;

import jxl.*;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;

public class ExcelWriter {

	private final String datapath = System.getProperty("user.dir") + "\\src\\Database\\";
	private final String filepath = datapath + "item.xls";

	public void saveDataToExcel(Vector <Item> itemList, boolean isNameWanted,boolean isPriceWanted,boolean isNecessityValueWanted,boolean isDayWanted, 
			boolean isMonthWanted, boolean isYearWanted,boolean isPlaceWanted, boolean isCategoryWanted, boolean isPaymentWanted) throws Exception{
		try{
			File exlFile = new File(filepath);
			WritableWorkbook writableWorkbook = Workbook.createWorkbook(exlFile);
			WritableSheet writableSheet = writableWorkbook.createSheet("Sheet1", 0);

			int n = -1;
			
			
			if(isNameWanted == true){
				Label label1 = new Label(++n,0,"Name");
				writableSheet.addCell(label1);
			}
			
			if(isPriceWanted == true){
				Label label2 = new Label(++n,0,"Price");
				writableSheet.addCell(label2);
			}
			
			if(isNecessityValueWanted == true){
				Label label3 = new Label(++n,0,"NecessityValue");
				writableSheet.addCell(label3);
			}
			
			if(isDayWanted == true){
				Label label4 = new Label(++n,0,"Day");
				writableSheet.addCell(label4);
			}
			
			if(isMonthWanted == true){
				Label label5 = new Label(++n,0,"Month");
				writableSheet.addCell(label5);
			}
			
			if(isYearWanted == true){
				Label label6 = new Label(++n,0,"Year");
				writableSheet.addCell(label6);
			}
			
			if(isPlaceWanted == true){
				Label label7 = new Label(++n,0,"Venue");
				writableSheet.addCell(label7);
			}
			
			if(isCategoryWanted == true){
				Label label8 = new Label(++n,0,"Category");
				writableSheet.addCell(label8);
			}
			
			if(isPaymentWanted == true){
				Label label9 = new Label(++n,0,"Payment");
				writableSheet.addCell(label9);
			}
						

			for(int i =0; i< itemList.size();i++){
				int m = -1;
				if(isNameWanted == true){
					Label tempLabel1 = new Label(++m,i+1,itemList.get(i).getName());
					writableSheet.addCell(tempLabel1);
				}
				
				if(isPriceWanted == true){
					Number tempNum1 = new Number(++m,i+1,itemList.get(i).getPrice());
					writableSheet.addCell(tempNum1);
				}
				
				if(isNecessityValueWanted == true){
					Number tempNum2 = new Number(++m,i+1,itemList.get(i).getNecessityValue());
					writableSheet.addCell(tempNum2);
				}
				
				if(isDayWanted == true){
					Number tempNum3 = new Number(++m,i+1,itemList.get(i).getDate().getDay());
					writableSheet.addCell(tempNum3);
				}
				
				if(isMonthWanted == true){
					Number tempNum4 = new Number(++m,i+1,itemList.get(i).getDate().getMonth());
					writableSheet.addCell(tempNum4);
				}
				
				if(isYearWanted == true){
					Number tempNum5 = new Number(++m,i+1,itemList.get(i).getDate().getYear());
					writableSheet.addCell(tempNum5);
				}
				
				if(isPlaceWanted == true){
					Label tempLabel2 = new Label(++m,i+1,itemList.get(i).getPlace());
					writableSheet.addCell(tempLabel2);
				}
				
				if(isCategoryWanted == true){
					Label tempLabel3 = new Label(++m,i+1,itemList.get(i).getCategory());
					writableSheet.addCell(tempLabel3);
				}
				
				if(isPaymentWanted == true){
					Label tempLabel4 = new Label(++m,i+1,itemList.get(i).getPay());
					writableSheet.addCell(tempLabel4);
				}
	
			}

			writableWorkbook.write();
			writableWorkbook.close();


		}catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}


}

